package topcbl.taxi;

import topcbl.task.SignInTask;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SignInFragment extends Fragment {
	Button btn_signin, btn_register, btn_help;
	EditText et_acc, et_pass;
	TextView link_register;
	View title, sologan, body, tail;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_signin, container,
				false);
		getResource(rootView);
		// ---
		btn_register.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent register = new Intent(getActivity(),
						RegisterActivity.class);
				startActivity(register);
			}
		});
		btn_signin.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (checkInputInfo()) {
					String acc = et_acc.getText() + "";
					String pass = et_pass.getText() + "";
					new SignInTask(getActivity(), acc, pass).execute();
				}
			}
		});
		return rootView;
	}

	private void getResource(View rootView) {
		btn_signin = (Button) rootView.findViewById(R.id.btn_splash_signin);
		btn_register = (Button) rootView.findViewById(R.id.btn_splash_abort);
		btn_help = (Button) rootView.findViewById(R.id.btn_splash_help);
		et_acc = (EditText) rootView.findViewById(R.id.splash_acc);
		et_pass = (EditText) rootView.findViewById(R.id.splash_pass);
		link_register = (TextView) rootView.findViewById(R.id.splash_link);
		title = (View) rootView.findViewById(R.id.splash_title);
		sologan = (View) rootView.findViewById(R.id.splash_sologan);
		body = (View) rootView.findViewById(R.id.splash_body);
		tail = (View) rootView.findViewById(R.id.splash_tail);
	}

	private boolean checkInputInfo() {
		if (et_acc.getText().length() > 0 && et_pass.getText().length() > 0) {
			return true;
		} else if (et_acc.getText().length() <= 0) {
			Toast.makeText(getActivity(),
					getString(R.string.splash_wrong_acc_input),
					Toast.LENGTH_SHORT).show();
			return false;
		} else if (et_pass.getText().length() <= 0) {
			Toast.makeText(getActivity(),
					getString(R.string.splash_wrong_pass_input),
					Toast.LENGTH_SHORT).show();
			return false;
		}
		return false;
	}
}